package com.moss.cloud.base.biz.config;

import com.moss.cloud.base.biz.service.IGatewayRoutesService;
import com.moss.cloud.base.biz.service.ISysDictService;
import com.moss.cloud.base.biz.service.ISysProvincesService;
import com.moss.cloud.base.biz.service.ISystemConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;

/**
 * 初始化缓存数据
 *
 * @Author: 瑾年
 * @Date: 2020-4-23
 */
@Configuration
@Slf4j
public class BaseInitializedEvent {
    private final IGatewayRoutesService gatewayRoutesService;
    private final ISysDictService sysDictService;
    private final ISystemConfigService systemConfigService;
    private final ISysProvincesService sysProvincesService;

    public BaseInitializedEvent(IGatewayRoutesService gatewayRoutesService,
                                ISysDictService sysDictService,
                                ISystemConfigService systemConfigService,
                                ISysProvincesService sysProvincesService) {
        this.gatewayRoutesService = gatewayRoutesService;
        this.sysDictService = sysDictService;
        this.systemConfigService = systemConfigService;
        this.sysProvincesService = sysProvincesService;
    }

    @Order
    @EventListener({WebServerInitializedEvent.class})
    public void initCache() {
        initGatewayCache();
        initDictCache();
        initSystemConfigCache();
        initProvinces();
    }

    private void initGatewayCache() {
        log.info("正在加载网关路由数据...");
        Boolean success = gatewayRoutesService.overload();
        log.info("初始化路由配置信息完成:{}", success);
    }

    private void initDictCache() {
        log.info("正在加载网关路由数据");
        Boolean dictFlag = sysDictService.overload();
        log.info("初始化路由配置信息完成:{}", dictFlag);
    }

    private void initSystemConfigCache() {
        log.info("初始化系统配置......");
        systemConfigService.loadSystemConfig();
        log.info("初始化路由配置信息完成!");
    }
    private void initProvinces(){
        log.info("初始化行政区划数据......");
        sysProvincesService.loadProvinces();
        log.info("初始化政区划数据完成!");
    }

}
